﻿namespace Grep.SharePoint.SmartDocuments
{
    using System;
    using Grep.Lib.Xml;
    using Microsoft.SharePoint;

    [Serializable]
    public sealed class TemplateLibraryConnectorSettings
    {
        [NonSerialized] private const string PROPERTY_KEY = "GREP_TLC_SETTINGS";

        public static TemplateLibraryConnectorSettings Default
        {
            get
            {
                return new TemplateLibraryConnectorSettings
                           {
                               DescriptionFieldName = "Description",
                               TemplateListId = "",
                               WebId = ""
                           };
            }
        }

        public string DescriptionFieldName { get; set; }
        public bool Enabled { get; set; }
        public string TemplateListId { get; set; }
        public string WebId { get; set; }

        public static TemplateLibraryConnectorSettings LoadFromList(SPList list)
        {
            // See if the property bag contains the settings' key
            if (!list.RootFolder.Properties.ContainsKey(PROPERTY_KEY))
            {
                return null;
            }
            return
                TinyXmlSerializer.Deserialize<TemplateLibraryConnectorSettings>(
                    list.RootFolder.Properties[PROPERTY_KEY] as string);
        }

        public static void SaveToList(SPList list, TemplateLibraryConnectorSettings settings)
        {
            list.RootFolder.Properties[PROPERTY_KEY] = TinyXmlSerializer.Serialize(settings);
            list.RootFolder.Update();
        }
    }
}